package Chapter6;

//133页程序6.4
interface Shape2D_3D {
    double pi = 3.14;  //数据成员一定要初始化

    double area(); //计算面积的抽象方法

    Double volume(); //计算体积的抽象方法，此处使用包装类
}

class Circle implements Shape2D_3D { //circle 圆形
    double radius;

    public Circle(double r) { //构造方法
        radius = r;
    }

    public double area() {
        return (pi * radius * radius); //计算面积
    }

    public Double volume() { //计算体积
        return null;
    }
}

class Rectangle implements Shape2D_3D { //rectangle 长方形
    double width, height, length;

    public Rectangle(double l, double w, double h) { //构造方法
        length = l; //长
        width = w; //宽
        height = h; //高
    }

    public Rectangle(double l, double w) { //构造方法
        length = l; //长
        width = w; //宽
    }

    public double area() {
        return (width * length); //计算面积
    }

    public Double volume() {
        return (length * width * height); //计算体积
    }
}

public class InterfaceTest {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(5, 6, 8);
        System.out.println("计算长方形rect的体积为：" + rect.volume());
        Rectangle rect2 = new Rectangle(5, 6);
        System.out.println("计算长方形rect的面积为：" + rect2.area());

        Circle cir = new Circle(2.5);
        System.out.println("计算圆形cir的面积为：" + cir.area());

//        接口还可以当作引用变量，访问子类的方法。
//        Shape2D_3D var1,var2,var3;
//        var1 = new Circle(2);
//        var2 = new Rectangle(2,3);
//        var3 = new Rectangle(2,3,4);
//        System.out.println(var1.area());
//        System.out.println(var2.area());
//        System.out.println(var3.volume());
    }
}
